package utils

import (
	"context"
	"fmt"
	"strconv"
	"time"

	"github.com/zhenghaoz/gorse/client"
)

// GorseClient 封装了 gorse 客户端，提供更友好的接口
type GorseClient struct {
	*client.GorseClient
	timeout time.Duration // 请求超时时间
}

// NewGorseClient 创建新的 Gorse 客户端实例
func NewGorseClient(host, apiKey string) *GorseClient {
	// 创建基础客户端
	gorseClient := client.NewGorseClient(host, apiKey)

	// 初始化自定义客户端
	c := &GorseClient{
		GorseClient: gorseClient,
		timeout:     5 * time.Second, // 默认超时时间
	}
	return c
}

// Option 客户端配置选项类型
type Option func(*GorseClient)

// WithTimeout 设置请求超时时间
func WithTimeout(timeout time.Duration) Option {
	return func(c *GorseClient) {
		c.timeout = timeout
	}
}

func (c *GorseClient) CreatUserGorse(ctx context.Context, userId int) error {
	// 使用 client.User 而不是自定义的 User
	userGorse := client.User{
		UserId: strconv.Itoa(userId),
	}

	row, err := c.GorseClient.InsertUser(ctx, userGorse)
	if err != nil {
		return fmt.Errorf("failed to insert user into Gorse: %w", err)
	}
	fmt.Printf("Inserted user into Gorse, affected rows: %d\n", row)
	return nil
}
